#include "Candidate.h"

Candidate::Candidate () {
  counter_Candidate = 0;
  testArray = (Test *) calloc (NUM_TEST_PER_CANDIDATE, sizeof(Test));
  bestTestNewPairs = -1;
}

Candidate::~Candidate () {
  free (testArray);
}

void Candidate::clear () {
  counter_Candidate = 0;
  memset (testArray, 0, NUM_TEST_PER_CANDIDATE * sizeof(Test));
  bestTestNewPairs = -1;
}

void Candidate::inputTest (Test testToSave) {
  // The number of new pairs is the same as the current highest number of new pairs, so add this test to the array
  if (testToSave.newPairs == bestTestNewPairs) {
    testArray[counter_Candidate++] = testToSave;
  }
  if (testToSave.newPairs > bestTestNewPairs) {
    bestTestNewPairs = testToSave.newPairs;
    counter_Candidate = 0;
    testArray[counter_Candidate++] = testToSave;
  }
}

// Chooses a test, updates the hash table
// Returns the best Test, randomly chooses if there is more than one best test
Test Candidate::chooseTest(int *hashArray) {
  srand((unsigned)time(0));
  int choiceIndex = rand() % counter_Candidate;

  testArray[choiceIndex].newPairs = 0;
  for (int n=0; n<testArray[choiceIndex].size; n++) {
    testArray[choiceIndex].set_Factor_Value (n, testArray[choiceIndex].testArray[n], hashArray);
  }

  return testArray[choiceIndex];
}
